package cn.com.self.apis.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;

/**
 * @Description json转换
 *
 * @date 2019-03-17
 */

public class JsonTransferUtil {

    /**
     *
     * @Description json字符串转对象
     * @Return 对象
     * @Date 2019/3/30 16:12
     */
    public static UserInfo jsonToObj(String jsonStr){
        if(jsonStr==null||"".equals(jsonStr)){
            return null;
        }
        UserInfo userInfo=new UserInfo();
        ObjectMapper mapper = new ObjectMapper();
        try {
            userInfo = mapper.readValue(jsonStr, userInfo.getClass());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return userInfo;
    }

    /**
     *
     * @Description 对象转换成json
     * @Return 字符串
     * @Date 2019/3/30 16:18
     */
    public static String objToJson(Object obj) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(obj);
    }
}
